package com.company;


import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;


/**
 * @date : 2022/3/25 16:47
 * Spring的配置文件, 相当于spring-config.xml
 */


@Configuration
@ComponentScan("com.company")
@PropertySource("classpath:application.properties")
@EnableAspectJAutoProxy
public class SpringConfiguration {

    @Value("${database.driver}")
    private String driver;

    @Value("${database.url}")
    private String url;

    @Value("${database.username}")
    private String username;

    @Value("${database.password}")
    private String password;

    /**
     * 创建一个数据源, 并存入Spring容器中
     *
     * @return 返回C3P0数据源
     */
    @Bean("dataSource")
    public DataSource dataSource() {
        try {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass(driver);
            dataSource.setJdbcUrl(url);
            dataSource.setUser(username);
            dataSource.setPassword(password);
            return dataSource;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @Bean("runner")
    @Scope("prototype")
    public QueryRunner queryRunner() {
        return new QueryRunner();
    }


//    @Bean("runner")
//    @Scope("prototype")
//    public QueryRunner queryRunner(@Qualifier("dataSource") DataSource dataSource) {
//        return new QueryRunner(dataSource);
//    }


//    @Bean("accountService")
//    public IAccountService accountService(@Qualifier("accountDao") AccountDaoImpl accountDao,
//                                          @Qualifier("transactionManager") TransactionManager transactionManager) {
//        // 1.定义被代理的对象
//        final IAccountService accountService = new AccountServiceImpl(accountDao);
//        // 2.创建代理对象
//        IAccountService proxyAccountService = (IAccountService) Proxy
//                .newProxyInstance(accountService.getClass().getClassLoader(),
//                        accountService.getClass().getInterfaces(),
//                        new InvocationHandler() {
//                            @Override
//                            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                                Object result = null;
//                                try {
//                                    // 1.开启事务
//                                    transactionManager.beginTransaction();
//                                    // 2.执行操作
//                                    result = method.invoke(accountService, args);
//                                    // 3.提交事务
//                                    transactionManager.commit();
//                                } catch (Exception e) {
//                                    // 4.回滚事务
//                                    transactionManager.rollback();
//                                    throw new Exception(e);
//                                } finally {
//                                    // 5.释放连接
//                                    transactionManager.release();
//                                }
//                                // 6.返回结果
//                                return result;
//                            }
//                        });
//        // 3.返回代理对象
//        return proxyAccountService;
//    }

}
